﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using celinda.Formulaire.Parametre;
using celinda.Formulaire.Tiers;
using celinda.Formulaire;

namespace celinda
{
    public partial class Celinda : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        #region Constructeur
        public Celinda()
        {
            InitializeComponent();
            Class.Utilisateur u = ((Class.Utilisateur)CallContext.GetData(Class.Const.UserConnected));
            tsLblUser.Text = u.Login;
            if (u.Societe != null)
                tsLblSociete.Text = u.Societe.Nom;
            else
                tsLblSociete.Text = "Sans Société";

        }
        #endregion

        #region TreeView
        private void btnSpecChercherMenu_Click(object sender, EventArgs e)
        {
            kTreeMenu.ExpandAll();
            if (txtChercheMenu.Text.Trim() == string.Empty)
                RetabliCouleurArrierePlanArbre(kTreeMenu.Nodes);
            else
                this.SearchInNodes(kTreeMenu.Nodes);
        }

        /// <summary>
        /// Permet de mettre en surbrillance ce que l'on recherche
        /// </summary>
        /// <param name="tnc">Le TreeNodeCollection dans lequel nous recherchons</param>
        private void SearchInNodes(TreeNodeCollection tnc)
        {
            foreach (TreeNode tn in tnc)
            {
                if (tn.Text.Contains(txtChercheMenu.Text))
                    tn.BackColor = Color.Yellow;
                else
                    tn.BackColor = Color.Transparent;
                SearchInNodes(tn.Nodes);
            }
        }

        /// <summary>
        /// Permet de remettre une couleur neutre à l'arrière plan du TreeView
        /// </summary>
        /// <param name="tnc">Le TreeNodeColection du TreeView</param>
        private void RetabliCouleurArrierePlanArbre(TreeNodeCollection tnc)
        {
            foreach (TreeNode tn in tnc)
            {
                tn.BackColor = Color.Transparent;
                RetabliCouleurArrierePlanArbre(tn.Nodes);
            }
        }

        private void kTreeMenu_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor; // Le pointeur devient un sablier
            if (kTreeMenu.SelectedNode != null)
            {
                if (IsChildNotOpen(kTreeMenu.SelectedNode.Name))
                {
                    switch (kTreeMenu.SelectedNode.Name)
                    {
                        case "NodeSociete":
                            SocieteForm socF = new SocieteForm();
                            socF.MdiParent = this;
                            socF.NameForm = kTreeMenu.SelectedNode.Name;
                            socF.Show();
                            break;
                        default: break;
                    }
                }
            }
            Cursor.Current = Cursors.Default;      // Le pointeur redevient la flèche
        }
        #endregion

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Celinda_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Class.Outils.ChoixOuiNon("Etes-vous sur de vouloir quitter ?") == System.Windows.Forms.DialogResult.No)
                e.Cancel = true;
        }

        private void utilisateurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UtilisateurForm uf = new UtilisateurForm();
            uf.MdiParent = this;
            uf.Show();
        }

        protected bool IsChildNotOpen(String NameForme)
        {
            bool cont = true;
            foreach (Form f in this.MdiChildren)
            {
                if (((BaseForm)f).NameForm != null)
                    if (((BaseForm)f).NameForm.Equals(NameForme))
                    {
                        f.BringToFront();
                        cont = false;
                        break;
                    }
            }
            return cont;
        }

    }
}
